package com.example.recyclerviewdemo.layoutmanager

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.recyclerviewdemo.R
import com.example.recyclerviewdemo.databinding.ItemDiffUtilBinding
import com.example.recyclerviewdemo.diffdemo.DiffUtilCallback
import com.example.recyclerviewdemo.model.bean.ChatBean
import com.example.recyclerviewdemo.utils.TimeUtils

class LinearLayoutAdapter(private val mContext: Context) :
    RecyclerView.Adapter<LinearLayoutAdapter.ItemViewHolder>() {

    private var items = mutableListOf<ChatBean>()   //数据源
    fun clearList() {
        items.clear()
        notifyDataSetChanged() //刷新所有列表
    }

    fun addList(list: MutableList<ChatBean>) {
        items.addAll(list)
        notifyItemRangeChanged(
            items.size - list.size - 1,
            list.size
        )  //刷新对应下标范围的数据  第一个参数的开始刷新的位置，第二个刷新item的个数
    }


    /**
     * 如果此时列表有几百上千条数据
     * 高频消息推送的情况下，会出现问题，所以需要进行异步比对数据
     * */
    fun setNewList(newList: MutableList<ChatBean>, diff: Boolean = false) {
        if(diff){
            val calculateDiff = DiffUtil.calculateDiff(DiffUtilCallback(items, newList), false)
            items = newList
            calculateDiff.dispatchUpdatesTo(this)
        }else{
            val mutableListOf = mutableListOf<ChatBean>()
            mutableListOf.addAll(newList)
            items.clear()
            items.addAll(mutableListOf)
            notifyDataSetChanged()
        }
    }

    fun getItems(): MutableList<ChatBean> {
        return items
    }

    /**
     * ViewHolder
     * */
    inner class ItemViewHolder(val binding: ItemDiffUtilBinding) :
        RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {

        val itemViewHolder = ItemViewHolder(
            DataBindingUtil.inflate(
                LayoutInflater.from(mContext),
                R.layout.item_diff_util,
                parent,
                false
            )
        )

        with(itemViewHolder.binding) {
            rootView.setOnClickListener {

            }
        }
        return itemViewHolder

    }

    override fun onBindViewHolder(
        holder: ItemViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {

        if(payloads.size >= 1){
            val bean = payloads[0]
            if(bean is Bundle){
                with(holder.binding) {
                    bean.getString("titleText", null)?.let {
                        titleText.text = it
                    }
                    bean.getString("contentText", null)?.let {
                        contentText.text = it
                    }
                    bean.getLong("time", -1L).let {
                        if(it != -1L){
                            timeText.text = TimeUtils.getChatListTime(it)
                        }
                    }
                }
            }
        }else{
            super.onBindViewHolder(holder, position, payloads)
        }
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val diffBean = items[position]
        with(holder.binding) {
            Glide.with(mContext).load(diffBean.iconUrl).into(iconImage)
            titleText.text = diffBean.titleText
            contentText.text = diffBean.contentText
            timeText.text = TimeUtils.getChatListTime(diffBean.time)
        }

    }

    override fun getItemCount(): Int = items.size


}